package com.wish.g1.accounting.controller;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import com.jeequan.jeepay.response.PayOrderQueryResponse;
import com.jeequan.jeepay.util.JeepayKit;
import com.wish.g1.accounting.config.JeepayConfig;
import com.wish.g1.accounting.entity.House;
import com.wish.g1.accounting.entity.PayOrder;
import com.wish.g1.accounting.entity.ReceiptPlan;
import com.wish.g1.accounting.service.IPayOrderService;
import com.wish.g1.accounting.service.IReceiptPlanService;
import com.wish.g1.accounting.util.AlipayUtils;
import com.wish.g1.accounting.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 支付订单表（支付流水信息） 前端控制器
 * </p>
 *
 * @author maolijun
 * @since 2022-11-18
 */
@Slf4j
@RestController
@RequestMapping("/payOrder")
public class PayOrderController {
    @Autowired
    AlipayUtils alipayUtils;

    @Autowired
    IPayOrderService payOrderService;

    @Autowired
    IReceiptPlanService receiptPlanService;

    @Autowired
    JeepayConfig jeepayConfig;


    @PostMapping("/notity")
    public void notity(@RequestBody Map<String,Object> map){
        log.info("----支付回调接口");
        log.info(String.valueOf(map));
    }

    @GetMapping("/taskPayOrder")
    public void taskPayOrder(){
        log.info("---开启定时任务");
        QueryWrapper<ReceiptPlan> wrapper = new QueryWrapper<>();
        wrapper.eq("sk_status",0);
        List<ReceiptPlan> list = receiptPlanService.list(wrapper);
        for (ReceiptPlan receiptPlan : list) {
            PayOrderQueryResponse response = alipayUtils.payOrderQuery(receiptPlan.getReceiptOrderNo());
            if (response != null && "SUCCESS".equals(response.getMsg())) {
                receiptPlan.setSkStatus(1);
                receiptPlanService.saveOrUpdate(receiptPlan);
                log.info("补单成功");
            } else {
                log.info("补单未成功");
            }
            log.info(String.valueOf(response));
        }
    }

    @RequestMapping("wishOrderList")
    public List<PayOrder> wishOrderList(){
        List<PayOrder> list = payOrderService.list();
        return list;
    }


//     @RequestMapping("pay")
//     public Result payOrder(@RequestBody House house){
//         PayOrder payOrder=new PayOrder();
//         Snowflake snowflake = IdUtil.getSnowflake(1,1);
//         long nextId = snowflake.nextId();
//
//         payOrder.setOrderNo(String.valueOf(nextId));
//         payOrder.setTradeAccount("peitianlong");
//         payOrder.setUserId(Long.valueOf(house.getUserId()));
//         payOrder.setUserType(1);
//         payOrder.setPayWay(2);
//         payOrder.setAmount(house.getRent());
//         payOrder.setCheckState(100);
//         payOrder.setDelFlg(false);
//         payOrder.setDzState(1);
//         payOrder.setCreateUserid(Long.valueOf(house.getUserId()));
//         payOrder.setCreateTime(LocalDateTime.now());
//         payOrder.setUpdateUserid(Long.valueOf(house.getUserId()));
//         payOrder.setUpdateTime(LocalDateTime.now());
//
//         payOrderService.save(payOrder);
//
//         PayOrderCreateResponse pay = alipayUtils.payOrder(payOrder.getOrderNo(),payOrder.getAmount().intValue(), house.getHouseName(), "支付");
// //        PayOrderCreateResponse pay = alipayUtils.payOrder(payOrder.getOrderNo(),100, "大兴小区", "支付");
//
//         return Result.ok(pay);
//     }

    @RequestMapping("pays")
    public Result payOrder(@RequestBody ReceiptPlan receiptPlan){
        PayOrder payOrder=new PayOrder();
        Snowflake snowflake = IdUtil.getSnowflake(1,1);

        payOrder.setOrderNo(String.valueOf(receiptPlan.getReceiptOrderNo()));
        payOrder.setTradeAccount("peitianlong");
        payOrder.setUserId(Long.valueOf(receiptPlan.getTenantId()));
        payOrder.setUserType(1);
        payOrder.setPayWay(2);
        payOrder.setAmount(receiptPlan.getPaymentAmount());
        payOrder.setCheckState(100);
        payOrder.setDelFlg(false);
        payOrder.setDzState(1);
        payOrder.setCreateUserid(Long.valueOf(receiptPlan.getTenantId()));
        payOrder.setCreateTime(LocalDateTime.now());
        payOrder.setUpdateUserid(Long.valueOf(receiptPlan.getTenantId()));
        payOrder.setUpdateTime(LocalDateTime.now());

        payOrderService.save(payOrder);

        PayOrderCreateResponse pay = alipayUtils.payOrder(payOrder.getOrderNo(),payOrder.getAmount().intValue(), "付款单结算", "支付");
//        PayOrderCreateResponse pay = alipayUtils.payOrder(payOrder.getOrderNo(),100, "大兴小区", "支付");

        return Result.ok(pay);
    }

}
